﻿
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace HoaDon
{
    public partial class ListInvoice : System.Web.UI.Page
    {
        private string sCommand = "";
        private System.Globalization.CultureInfo cultureinfo = new System.Globalization.CultureInfo("vi-VN");
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                if (Session["NgayBD"] != null)
                {
                    sCommand += " and Ngay_ct >='" + DateTime.Parse(Session["NgayBD"].ToString(), cultureinfo).ToString("yyyyMMdd") + "' ";
                }
                if (Session["NgayKT"] != null)
                {
                    sCommand += " and Ngay_ct <='" + DateTime.Parse(Session["NgayKT"].ToString(), cultureinfo).ToString("yyyyMMdd") + "' ";
                }

                if (Session["btKhuVuc"] != null)
                {
                    if (Session["btKhuVuc"].ToString() != "All")
                        if (Session["btBan"].ToString() != "AllB")
                        {
                            sCommand += " AND FK_VvID = '" + Session["btBan"].ToString() + "'";
                        }
                        else
                        {
                            sCommand += " AND FK_VvID IN (SELECT vvID FROM dmvv WHERE dmvv.FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND FK_NhvvID = '" + Session["btKhuVuc"].ToString() + "' )";
                        }
                }


                if (Session["btNhanVien"] != null)
                {
                    if (Session["btNhanVien"].ToString() != "All")
                        sCommand += " AND s3 = '" + Session["btNhanVien"].ToString() + "'";
                }

                if (Session["KhoTK"] != null)
                {
                    if (Session["KhoTK"].ToString() != "All")
                        sCommand += " AND FK_khoID = '" + Session["KhoTK"].ToString() + "'";
                }

                if (Session["HTToanBo"] != null)
                    if (Session["HTToanBo"].ToString().Equals("0"))
                        sCommand += " AND Status = '1'";
				sCommand = sCommand + " AND Status != '*'";
                DataTable ds = new DataTable();

                if (Session["TypeSearch"] != null)
                {
                    if (Session["TypeSearch"] == "HD")
                        ds = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM vph81 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"] + "' AND 1=1 " + sCommand + " ORDER BY Ngay_Ct");
                    if (Session["TypeSearch"] == "XK")
                        ds = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM vph84 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"] + "' AND 1=1 " + sCommand + " ORDER BY Ngay_Ct");
                    if (Session["TypeSearch"] == "NK")
                        ds = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM vph74 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"] + "' AND 1=1 " + sCommand + " ORDER BY Ngay_Ct");

                }
                else
                {
                    Response.Redirect("~\\Invoice.aspx");
                    return;
                }

                Session["dtsds"] = ds;
                if (ds != null)
                {
                    if (ds.Rows.Count > 0)
                    {
                        decimal ttien = 0;
                        for (int i = 0; i < ds.Rows.Count; i++)
                        {
                            ttien += Convert.ToDecimal(ds.Rows[i]["T_Tien"].ToString());
                        }
                        DataRow dr = ds.NewRow();
                        dr["Stt_rec"] = "Tổng tiền";
                        dr["T_Tien"] = "" + ttien;
                        ds.Rows.Add(dr);
                    }
                    gdxdshoadon.DataSource = ds;
                    gdxdshoadon.DataBind();
                    if (ds.Rows.Count > 0)
                        gdxdshoadon.Rows[ds.Rows.Count - 1].Cells[8].Enabled = false;
                    gdxdshoadon.CssClass = "GridViewInvoice";
                }
            }
        }

        protected void btXoa_Click(object sender, EventArgs ap)
        {
            //ImageButton bt = (ImageButton)sender;
            ////DeletePXA(bt.CommandArgument);
            //SqlEXEC.ExecCommand(Session["strConn"].ToString(), "EXEC [fs_DeleteHDA] '" + bt.CommandArgument + "','" + Session["FK_DatabaseID"].ToString() + "', '1'");

            //Response.Redirect("~\\ListInvoice.aspx");
        }

        private void DeletePXA(string stt_rec_HDA)
        {
            //string sStt_rec_PXA = "";
            //DataSet ds2 = new DataSet();
            //sql.SQLRetrieve(appConn, "select stt_rec from ph84 where s1 = '" + stt_rec_HDA.Trim() + "'", "recPXA", ref ds2);

            //if (ds2 != null)
            //{
            //    if (ds2.Tables.Count > 0)
            //    {
            //        if (ds2.Tables["recPXA"].Rows.Count > 0)
            //        {
            //            //Delete ct00, ct70
            //            sStt_rec_PXA = ds2.Tables["recPXA"].Rows[0]["stt_rec"].ToString();
            //            string text2 = "EXEC fs_DeletePXA ";
            //            text2 = (text2 + "'" + sStt_rec_PXA.Trim() + "'");
            //            text2 = (text2 + ", '" + "1" + "'");
            //            sql.SQLExecute(ref appConn, text2, 30);

            //            //Delete ph84, ct84
            //            text2 = "delete from ct84 where stt_rec = '" + sStt_rec_PXA + "'";
            //            sql.SQLExecute(ref appConn, text2, 30);

            //            text2 = "delete from ph84 where stt_rec = '" + sStt_rec_PXA + "'";
            //            sql.SQLExecute(ref appConn, text2, 30);
            //        }
            //    }
            //}
        }

        protected void btXem_Click(object sender, EventArgs ap)
        {
            ImageButton bt = (ImageButton)sender;
            string hoadon_tien = bt.CommandArgument;
            string[] s = hoadon_tien.Split('_');
            Session["HoaDonID"] = s[0];
            Session["TTien"] = s[1];

            Response.Redirect("~\\DSMon.aspx");
        }

        protected void btIn_Click(object sender, EventArgs e)
        {
            DataTable ds = Session["dtsds"] as DataTable;
            rptBCBKBH rpt = new rptBCBKBH { DataSource = ds };
            rpt.ma_tu_dong.DataBindings.Add("Text", ds, "STT_rec");
            rpt.ngay_ct.DataBindings.Add("Text", ds, "Ngay_ct", "{0:dd/MM/yyyy}");
            rpt.ban.DataBindings.Add("Text", ds, "vvName");
            rpt.tong_tien.DataBindings.Add("Text", ds, "T_tien", "{0:#,#}");
            rpt.nguoi_tao.DataBindings.Add("Text", ds, "user_id0");
            rpt.ngay_tao.DataBindings.Add("Text", ds, "datetime0", "{0:dd/MM/yyyy}");
            rpt.nguoi_sua.DataBindings.Add("Text", ds, "user_id2");
            rpt.ngay_sua.DataBindings.Add("Text", ds, "datetime2", "{0:dd/MM/yyyy}");

            if (Session["NgayBD"] != null)
                rpt.Parameters["tungay"].Value = Session["NgayBD"].ToString();
            else
                rpt.Parameters["tungay"].Value = "";
            if (Session["NgayKT"] != null)
                rpt.Parameters["denngay"].Value = Session["NgayKT"].ToString();
            else
                rpt.Parameters["denngay"].Value = "";
            rpt.Parameters["khuvuc"].Value = Session["TenKhuVuc"].ToString();
            rpt.Parameters["tenban"].Value = Session["TenBan"].ToString();
            Session["Report"] = rpt;
            Response.Redirect("~\\Print.aspx");
        }

    }
}
